package com.h3c.web.rest;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.h3c.dal.common.TableConstant;
import com.h3c.dal.dao.CasssoOrganizationUnitDAO;
import com.h3c.dal.dao.CasssoSystemDAO;
import com.h3c.dal.entity.CasssoOrganizationUnitDO;
import com.h3c.dal.entity.CasssoSystemDO;
import com.h3c.dal.entity.query.CasssoOrganizationUnitQuery;
import com.h3c.dal.entity.query.CasssoSystemQuery;
import com.h3c.web.common.BaseResponse;
import com.h3c.web.common.ErrorMsgEnum;
import com.h3c.web.common.constant.RoleConstant.RoleEnum;
import com.h3c.web.common.constant.SessionConstant;
import com.h3c.web.common.helper.UserHelper;
import com.h3c.web.common.utils.BeanUtil;
import com.h3c.web.common.utils.BindingResultUtil;
import com.h3c.web.common.utils.ListUtil;
import com.h3c.web.common.utils.UUIDUtil;
import com.h3c.web.common.vo.CasssoOrganizationUnitVO;
import com.h3c.web.common.vo.CasssoSystemVO;
import com.h3c.web.support.console.RoleSupport;

/**
 * @ClassName: SysRest
 * @Description: 系统接入api
 * @author: d14287
 * @date: 2017年5月31日 下午3:49:36
 */
@Controller
@RequestMapping("console/sys")
public class SysRest {
	public static final Logger logger = LoggerFactory.getLogger(SysRest.class);
	@Autowired
	private CasssoSystemDAO casssoSystemDAO;
	@Autowired
	private CasssoOrganizationUnitDAO casssoOrganizationUnitDAO;
	@Autowired
	private RoleSupport roleSupport;

	@RequestMapping(value = "/add.ajax", method = RequestMethod.POST)
	public @ResponseBody
	BaseResponse add(@RequestBody @Valid CasssoSystemVO casssoSystemVO,
			BindingResult bindingResult, HttpServletRequest request)
			throws Exception {
		if (bindingResult.hasErrors()) {
			return BaseResponse.falseOf(BindingResultUtil
					.assembleErrMsg(bindingResult));
		}
		String roleId = (String) request.getSession().getAttribute(
				SessionConstant.ROLE_ID);
		RoleEnum role = roleSupport.getRole(roleId);
		if (RoleEnum.super_admin.name().equals(role.name())) {
			if (StringUtils.isBlank(casssoSystemVO.getOuId())) {
				return BaseResponse.falseOf(ErrorMsgEnum.param_ou_is_blank
						.getMsg());
			}
		}
		if (RoleEnum.org_admin.name().equals(role.name())) {
			String ouId = (String) request.getSession().getAttribute(
					SessionConstant.OU_ID);
			casssoSystemVO.setOuId(ouId);
		}
		CasssoSystemDO casssoSystemDO = new CasssoSystemDO();
		BeanUtil.copyProperties(casssoSystemVO, casssoSystemDO);
		casssoSystemDO.setSysId(UUIDUtil.generateUUID());
		casssoSystemDO.setCreateTime(new Date());
		casssoSystemDO.setCreater(UserHelper.get());
		casssoSystemDO.setStatus(TableConstant.COMMON_STATUS_NORMAL);
		casssoSystemDAO.insert(casssoSystemDO);
		return BaseResponse.trueOf();
	}

	@RequestMapping(value = "/edit.ajax", method = RequestMethod.POST)
	public @ResponseBody
	BaseResponse edit(@RequestBody @Valid CasssoSystemVO casssoSystemVO,
			BindingResult bindingResult, HttpServletRequest request)
			throws Exception {
		if (bindingResult.hasErrors()) {
			return BaseResponse.falseOf(BindingResultUtil
					.assembleErrMsg(bindingResult));
		}
		if (StringUtils.isBlank(casssoSystemVO.getSysId())) {
			return BaseResponse.falseOf(ErrorMsgEnum.sysId_cannot_blank
					.getMsg());
		}
		String roleId = (String) request.getSession().getAttribute(
				SessionConstant.ROLE_ID);
		RoleEnum role = roleSupport.getRole(roleId);
		CasssoSystemQuery sysQuery = new CasssoSystemQuery();
		if (RoleEnum.super_admin.name().equals(role.name())) {
			sysQuery.createCriteria()
					.andSysIdEqualTo(casssoSystemVO.getSysId());
		}
		if (RoleEnum.org_admin.name().equals(role.name())) {
			String ouId = (String) request.getSession().getAttribute(
					SessionConstant.OU_ID);
			sysQuery.createCriteria()
					.andSysIdEqualTo(casssoSystemVO.getSysId())
					.andOuIdEqualTo(ouId);
		}
		CasssoSystemDO casssoSystemDO = new CasssoSystemDO();
		BeanUtil.copyProperties(casssoSystemVO, casssoSystemDO);
		casssoSystemDO.setSysId(null);
		casssoSystemDAO.updateByExampleSelective(casssoSystemDO, sysQuery);
		return BaseResponse.trueOf();
	}

	@RequestMapping(value = "/delete.ajax", method = RequestMethod.POST)
	public @ResponseBody
	BaseResponse delete(@RequestParam("sysId") String sysId,
			HttpServletRequest request) throws Exception {
		if (StringUtils.isBlank(sysId)) {
			return BaseResponse.falseOf(ErrorMsgEnum.sysId_cannot_blank
					.getMsg());
		}
		String roleId = (String) request.getSession().getAttribute(
				SessionConstant.ROLE_ID);
		RoleEnum role = roleSupport.getRole(roleId);
		CasssoSystemQuery sysQuery = new CasssoSystemQuery();
		if (RoleEnum.super_admin.name().equals(role.name())) {
			sysQuery.createCriteria().andSysIdEqualTo(sysId);
		}
		if (RoleEnum.org_admin.name().equals(role.name())) {
			String ouId = (String) request.getSession().getAttribute(
					SessionConstant.OU_ID);
			sysQuery.createCriteria().andOuIdEqualTo(ouId)
					.andSysIdEqualTo(sysId);
		}
		CasssoSystemDO sysDO = new CasssoSystemDO();
		sysDO.setStatus(TableConstant.COMMON_STATUS_DELETE);
		casssoSystemDAO.updateByExampleSelective(sysDO, sysQuery);
		return BaseResponse.trueOf();
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/fuzzyQuery.ajax", method = RequestMethod.GET)
	public @ResponseBody
	BaseResponse fuzzyQuery(@RequestParam("key") String key,
			HttpServletRequest request) throws Exception {
		String roleId = (String) request.getSession().getAttribute(
				SessionConstant.ROLE_ID);
		CasssoSystemQuery sysQuery = new CasssoSystemQuery();
		RoleEnum role = roleSupport.getRole(roleId);
		switch (role) {
		case super_admin:
			sysQuery.createCriteria().andSysNameLike("%" + key + "%")
					.andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
			break;
		case org_admin:
			String ouId = (String) request.getSession().getAttribute(
					SessionConstant.OU_ID);
			sysQuery.createCriteria().andOuIdEqualTo(ouId)
					.andSysNameLike("%" + key + "%")
					.andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
			break;
		default:
			break;
		}
		List<CasssoSystemDO> sysDOList = casssoSystemDAO
				.selectByExample(sysQuery);
		List<CasssoSystemVO> sysVOList = ListUtil.copyList(sysDOList,
				CasssoSystemVO.class);
		if (ListUtil.isEmpty(sysVOList)) {
			return BaseResponse.trueOf();
		}
		for (CasssoSystemVO vo : sysVOList) {
			CasssoOrganizationUnitQuery orgQuery = new CasssoOrganizationUnitQuery();
			orgQuery.createCriteria().andOuIdEqualTo(vo.getOuId())
					.andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
			List<CasssoOrganizationUnitDO> orgDOList = casssoOrganizationUnitDAO
					.selectByExample(orgQuery);
			if (ListUtil.isEmpty(orgDOList)) {
				continue;
			}
			CasssoOrganizationUnitVO orgVO = new CasssoOrganizationUnitVO();
			BeanUtil.copyProperties(orgDOList.get(0), orgVO);
			vo.setCasssoOrganizationUnitVO(orgVO);
		}
		return BaseResponse.trueOf(sysVOList);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/pageQuery.ajax", method = RequestMethod.GET)
	public @ResponseBody
	BaseResponse listQuery(HttpServletRequest request) throws Exception {
		String roleId = (String) request.getSession().getAttribute(
				SessionConstant.ROLE_ID);
		CasssoSystemQuery sysQuery = new CasssoSystemQuery();
		RoleEnum role = roleSupport.getRole(roleId);
		switch (role) {
		case super_admin:
			sysQuery.createCriteria().andStatusEqualTo(
					TableConstant.COMMON_STATUS_NORMAL);
			break;
		case org_admin:
			String ouId = (String) request.getSession().getAttribute(
					SessionConstant.OU_ID);
			sysQuery.createCriteria().andOuIdEqualTo(ouId)
					.andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
			break;
		default:
			break;
		}
		List<CasssoSystemDO> sysDOList = casssoSystemDAO
				.selectByExample(sysQuery);
		List<CasssoSystemVO> sysVOList = ListUtil.copyList(sysDOList,
				CasssoSystemVO.class);
		if (ListUtil.isEmpty(sysVOList)) {
			return BaseResponse.trueOf();
		}
		for (CasssoSystemVO vo : sysVOList) {
			CasssoOrganizationUnitQuery orgQuery = new CasssoOrganizationUnitQuery();
			orgQuery.createCriteria().andOuIdEqualTo(vo.getOuId())
					.andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
			List<CasssoOrganizationUnitDO> orgDOList = casssoOrganizationUnitDAO
					.selectByExample(orgQuery);
			if (ListUtil.isEmpty(orgDOList)) {
				vo.setCasssoOrganizationUnitVO(new CasssoOrganizationUnitVO());
				continue;
			}
			CasssoOrganizationUnitVO orgVO = new CasssoOrganizationUnitVO();
			BeanUtil.copyProperties(orgDOList.get(0), orgVO);
			vo.setCasssoOrganizationUnitVO(orgVO);
		}
		return BaseResponse.trueOf(sysVOList);
	}
}
